﻿// 功能：屏幕设备上下文
// 描述：
// 作者：温伟鹏
// 日期：2010/5/26 14:39:17

using System;
using System.Runtime.InteropServices;
using System.Globalization;
using System.ComponentModel;

namespace MeizuSDK.Drawing
{
    /// <summary>
    /// 屏幕设备上下文。用于直接在屏幕上绘图
    /// </summary>
    public class NativeScreenDC
    {
        // Fields
        private IntPtr handle = UnsafeNativeMethods.GetDC(IntPtr.Zero);

        /// <summary>
        /// 获取屏幕设备上下文句柄
        /// </summary>
        public IntPtr Handle
        {
            get
            {
                return this.handle;
            }
        }

        /// <summary>
        /// 构建本地屏幕设备上下文
        /// </summary>
        public NativeScreenDC()
        {
            if (this.handle == IntPtr.Zero)
            {
                int lastError = Marshal.GetLastWin32Error();
                throw new Win32Exception(0, "GetLastError=" + lastError.ToString(CultureInfo.InvariantCulture));
            }
        }
        /// <summary>
        /// 销毁资源
        /// </summary>
        public void Dispose()
        {
            this.DisposeResources();
            GC.SuppressFinalize(this);
        }
        /// <summary>
        /// 销毁资源
        /// </summary>
        private void DisposeResources()
        {
            if (this.handle != IntPtr.Zero)
            {
                if (UnsafeNativeMethods.ReleaseDC(IntPtr.Zero, this.handle) == 0)
                {
                    int lastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(0, "GetLastError=" + lastError.ToString(CultureInfo.InvariantCulture));
                }
                this.handle = IntPtr.Zero;
            }
        }
        /// <summary>
        /// 析构函数
        /// </summary>
        ~NativeScreenDC()
        {
            this.DisposeResources();
        }
    }
}
